<?php
/**
 * 属性管理 ，可以添加两级分类，任意动态添加属性。
 * 入库麻烦和读取麻烦。
 * 张名尊， 2014-04-29
 */ 

class attributeAction extends backendAction {
    public function _initialize() {
        parent::_initialize();
        $this->_mod = D('attribute');
    }

    public function index() {
        $sort = $this->_request("sort", 'trim', 'ordid');
        $order = $this->_request("order", 'trim', 'ASC');
        $tree = new Tree();
        $tree->icon = array('│ ','├─ ','└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $result = $this->_mod->order($sort . ' ' . $order)->select();
        $array = array();
        foreach($result as $r) {          
            $r['str_status'] = '<img data-tdtype="toggle" data-id="'.$r['id'].'" data-field="status" data-value="'.$r['status'].'" src="__STATIC__/images/admin/toggle_' . ($r['status'] == 0 ? 'disabled' : 'enabled') . '.gif" />';
            $r['str_manage'] = ''; 
            $width = $r['pid']?'700':'500';
            $height = $r['pid']?'400':'50';          
            if($r['pid']==0){
                $r['str_manage'] .= '<a href="javascript:;" class="J_showdialog" data-uri="'.U('attribute/oppt',array('opttype'=>'add','pid'=>$r['id'])).'" data-title="'.L('add_attribute').'" data-id="add" data-width="700" data-height="'.$height.'">'.L('add_attribute').'</a>';
            }
            $width = $r['pid']?'700':'500';
            $r['str_manage'] .= ' |
                                <a href="javascript:;" class="J_showdialog" data-uri="'.U('attribute/oppt',array('opttype'=>'edit','pid'=>$r['pid'],'id'=>$r['id'])).'" data-title="'.L('edit').' - '. $r['name'] .'" data-id="edit" data-width="'.$width.'" data-height="'.$height.'">'.L('edit').'</a> |
                                <a href="javascript:;" data-acttype="ajax" class="J_confirmurl" data-uri="'.U('attribute/delete',array('id'=>$r['id'])).'" data-msg="'.sprintf(L('confirm_delete_one'),$r['name']).'">'.L('delete').'</a>';
            $r['parentid_node'] = ($r['pid'])? ' class="child-of-node-'.$r['pid'].'"' : '';
            
            $array[] = $r;
        }
        $str  = "<tr id='node-\$id' \$parentid_node>
                <td align='center'><input type='checkbox' value='\$id' class='J_checkitem'></td>
                <td>\$spacer<span data-tdtype='edit' data-field='name' data-id='\$id' class='tdedit'>\$name</span></td>
                <td align='center'>\$id</td>
              
                <td align='center'><span data-tdtype='edit' data-field='ordid' data-id='\$id' class='tdedit'>\$ordid</span></td>

                <td align='center'>\$str_status</td>
                <td align='center'>\$str_manage</td>
                </tr>";
        $tree->init($array);
        $list = $tree->get_tree(0, $str);
        $this->assign('list', $list);
        
         
        $big_menu = array(
            'title' =>'添加分类',
            'iframe' => U('attribute/oppt',array('opttype'=>'add')),
            'id' => 'add',
            'width' => '500',
            'height' => '50'
        );
        $this->assign('big_menu', $big_menu);
        $this->assign('list_table', true);
        $this->display();
    }
    
    public function oppt(){
        $type = $this->_request('opttype');
        $this->assign('type',$type);
        $this->readyData();
        $this->$type();
        $this->display();
    }
 
    private function readyData(){
        if(!IS_POST){
            $pid = $this->_get('pid', 'intval', 0);
            $classList = $this->_mod->where(array('pid'=>0))->getField('id,name');
            $this->assign('classList',$classList);
            $this->assign('pid',$pid);
        }
              
    }

    /**
     * 入库数据整理
     */
    protected function _before_insert($data = '') {
        $data['rules'] = json_encode($data['rules']);
        return $data;
    }

    /**
     * 修改提交对数据
     */
    protected function _before_update($data = '') {    
        $data['rules'] = json_encode($data['rules']);
        return $data;
    }

}